angular.module('myService',[])
.factory('cart',[function(){
	var obj = {flag:false};
	obj.dataCart = {};

	obj.addFun = function(i,text){
		obj.flag = true;
		i.status = true;
		obj.dataCart[text+""+i.id] = i;
	}
	
	obj.delFun = function(id){
		delete obj.dataCart[id];
		console.log(obj.dataCart);
		if (JSON.stringify(obj.dataCart) == "{}") {
			obj.flag = false;
		}
	}
	return obj;
}])

.factory('selectService',['cart',function(cart){
	var obj = {};
	obj.selectAll = function(statu){
		for (var i in cart.dataCart) {
			cart.dataCart[i].status = statu;
		}
	}
	obj.danxuan = function(){
		var _statu = true;
		for (var i in cart.dataCart) {
			if (!cart.dataCart[i].status) {
				_statu = false;
				// console.log(statu);
			} 
		}
		return _statu;
	}
	return obj;

}])

.factory('total',['cart',function(cart){
	var obj = {};
	obj.count = 0;
	obj.money = 0;
	obj.fun = function(){
		var count = 0;
		var money = 0;
		for(var i in cart.dataCart){
			count += cart.dataCart[i].num;
			if (cart.dataCart[i].status) {
				money += (cart.dataCart[i].num * (cart.dataCart[i].price*100))/100;
				console.log(money);
			}
		}
		obj.count = count;
		obj.money = money;
	}
	return obj;
}])